package com.example.azheng.rxjavamvpdemo.ui.acitivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.internal.BottomNavigationItemView;
import android.support.design.internal.BottomNavigationMenuView;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;

import com.example.azheng.rxjavamvpdemo.R;
import com.example.azheng.rxjavamvpdemo.ui.fragment.HomeFragment;
import com.example.azheng.rxjavamvpdemo.ui.fragment.MineFragment;
import com.example.azheng.rxjavamvpdemo.ui.fragment.PromoteFragment;
import com.example.azheng.rxjavamvpdemo.ui.fragment.RechargeFragment;
import com.example.azheng.rxjavamvpdemo.ui.fragment.WithdrawFragment;
import com.example.azheng.rxjavamvpdemo.util.UserInfoUtils;

import java.lang.reflect.Field;


public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomNavigationView;
    //定义Fragment
    private HomeFragment mHomeFragment;
    private RechargeFragment mRechargeFragment;
    private WithdrawFragment mWithdrawFragment;
    private PromoteFragment mPromoteFragment;
    private MineFragment mMineFragment;

    //记录当前正在使用的fragment
    private Fragment isFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化Fragment及底部导航栏
        initFragment(savedInstanceState);
        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomNav);
        //关闭底部导航栏默认动画效果并添加监听器
        disableShiftMode(bottomNavigationView);
        bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

    public void initFragment(Bundle savedInstanceState) {

        Log.i("MainActivity", "token:" + UserInfoUtils.getToken());

        //判断activity是否重建，如果不是，则不需要重新建立fragment.
        if (savedInstanceState == null) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            if (mHomeFragment == null) {
                mHomeFragment = new HomeFragment();
            }
            isFragment = mHomeFragment;
            ft.replace(R.id.container, mHomeFragment).commit();
        }
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    if (mHomeFragment == null) {
                        mHomeFragment = new HomeFragment();
                    }
                    switchContent(isFragment, mHomeFragment);
                    return true;
                case R.id.navigation_two:
                    if (mRechargeFragment == null) {
                        mRechargeFragment = new RechargeFragment();
                    }
                    switchContent(isFragment, mRechargeFragment);
                    return true;
                case R.id.navigation_three:
                    if (mWithdrawFragment == null) {
                        mWithdrawFragment = new WithdrawFragment();
                    }
                    switchContent(isFragment, mWithdrawFragment);
                    return true;
                case R.id.navigation_four:
                    if (mPromoteFragment == null) {
                        mPromoteFragment = new PromoteFragment();
                    }
                    switchContent(isFragment, mPromoteFragment);
                    return true;
                case R.id.navigation_five:
                    if (mMineFragment == null) {
                        mMineFragment = new MineFragment();
                    }
                    switchContent(isFragment, mMineFragment);
                    return true;
            }
            return false;
        }

    };


    public void switchContent(Fragment from, Fragment to) {
        if (isFragment != to) {
            isFragment = to;
            FragmentManager fm = getSupportFragmentManager();
            //添加渐隐渐现的动画
            FragmentTransaction ft = fm.beginTransaction();
            if (!to.isAdded()) {    // 先判断是否被add过
                ft.hide(from).add(R.id.container, to).commit(); // 隐藏当前的fragment，add下一个到Activity中
            } else {
                ft.hide(from).show(to).commit(); // 隐藏当前的fragment，显示下一个
            }
        }
    }

    //利用反射关闭底部导航栏默认动画效果，使多个按钮平分界面
    @SuppressLint("RestrictedApi")
    public void disableShiftMode(BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            Field shiftingMode = menuView.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(menuView, false);
            shiftingMode.setAccessible(false);
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
                //noinspection RestrictedApi
                item.setShiftingMode(false);
                // set once again checked value, so view will be updated
                //noinspection RestrictedApi
                item.setChecked(item.getItemData().isChecked());
            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }

}
